package com.ruicar.afs.cloud.apply.applytocontract;

import com.ruicar.afs.cloud.apply.common.utils.ApplyConfig;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.ContractFeginParamEnum;
import com.ruicar.afs.cloud.manage.outer.api.insurance.dto.ManageInsuranceInfoDto;
import com.ruicar.afs.cloud.manage.outer.api.insurance.feign.InsuranceForApplyFeign;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xudongdong
 * @date 2020-06-16
 */
@Slf4j
@RestController
@RequestMapping("/insuranceContract")
@AllArgsConstructor
public class ContractInsuranceController {

    private final InsuranceForApplyFeign insuranceForApplyFeign;

    private final ApplyConfig applyConfig;

    @PostMapping("/getInsuranceInfoList")
    @ApiOperation(value = "获取保险业务数据 多条件")
    @Transactional(rollbackFor = Exception.class)
    public IResponse getInsuranceInfoList(@RequestBody QueryCondition<ManageInsuranceInfoDto> insuranceClaimCondition) {
        return insuranceForApplyFeign.getInsuranceInfoList(insuranceClaimCondition, heards());
    }

    @PostMapping("/saveInsurance")
    @ApiOperation(value = "保存保险业务信息")
    @Transactional(rollbackFor = Exception.class)
    public IResponse saveInsurance(@RequestBody ManageInsuranceInfoDto insuranceClaimCondition) {
        return insuranceForApplyFeign.saveInsurance(insuranceClaimCondition, heards());
    }

    @PostMapping("/submitInsurance")
    @ApiOperation(value = "提交保险业务信息")
    @Transactional(rollbackFor = Exception.class)
    public IResponse submitInsurance(@RequestBody ManageInsuranceInfoDto insuranceClaimCondition) {
        return insuranceForApplyFeign.submitInsurance(insuranceClaimCondition, heards());
    }

    @ApiOperation(value = "根据合同号码查询保险信息")
    @PostMapping("/getInsuranceData")
    public IResponse getInsuranceData(@RequestParam("id") Long id) {
        return insuranceForApplyFeign.getInsuranceData(id, heards());
    }

    @ApiOperation(value = "获取处理意见数据")
    @PostMapping("/getApprovalOpinion")
    public IResponse getApprovalOpinion(@RequestParam("contractNo") String contractNo, @RequestParam("caseNo") String caseNo) {
        return insuranceForApplyFeign.getApprovalOpinion(contractNo, caseNo, heards());
    }

    @Transactional
    @SysLog("删除保险业务信息")
    @ApiOperation(value = "通过id删除")
    @RequestMapping(value = "/delById/{id}", method = RequestMethod.DELETE)
    public IResponse<Boolean> delById(@PathVariable String id) {
        return insuranceForApplyFeign.delById(id, heards());
    }

    @PostMapping("/PrintTemplate")
    @ApiOperation(value = "打印无章授权文件模板")
    @Transactional(rollbackFor = Exception.class)
    public IResponse PrintTemplate(@RequestBody ManageInsuranceInfoDto insuranceClaimCondition) {
        return insuranceForApplyFeign.PrintTemplate(insuranceClaimCondition, heards());
    }

    @PostMapping("/PrintHaveTemplate")
    @ApiOperation(value = "打印有章授权文件模板")
    @Transactional(rollbackFor = Exception.class)
    public IResponse PrintHaveTemplate(HttpServletResponse response, @RequestBody ManageInsuranceInfoDto insuranceClaimCondition) {
        return insuranceForApplyFeign.PrintHaveTemplate(insuranceClaimCondition, heards());
    }

    private Map heards() {
        Map<String, String> heards = new HashMap<>(16);
        heards.put(AfsEnumUtil.key(ContractFeginParamEnum.BASIC_CLIENT_ID), applyConfig.getContractClientId());
        heards.put(AfsEnumUtil.key(ContractFeginParamEnum.BASIC_CLIENT_SECRET), applyConfig.getContractClientSecret());
        return heards;
    }
}
